<?php
// +----------------------------------------------------------------------
// | YotakuCMS
// +----------------------------------------------------------------------
// | Copyright (c) 2016 http://www.yotaku.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: Creatint <creatint@163.com> 
// +----------------------------------------------------------------------

namespace Home\Controller;

/**
 * 文档模型控制器
 * 文档模型列表和详情
 */
class TmallController extends HomeController {

	/* 文档模型频道页 */
	public function index(){
		if(session("type") != 'Tmall'){
			$this->error('未登录或无权限，请重新确认登陆身份！',U('Home/User/login/type/'.session('type')));
		}
		$row = C('ROW');
		$this->assign('controller','Tmall');
		$this->assign('action','index');


		/* 获取扩展模型数据 */
		$logic  = D('Document')->logic(4);

		//dump($_GET);
		//是否搜索
		$sql = '';
		$i = 0;
		$_GET['cashcoupon'] = 2;//只显示已审核已领取的记录
		foreach ($_GET as $k=>$v){
			if($i>0 && 'p' != $k) $sql .= " AND ";
			if('p' != $k) $sql .= "`{$k}` like '%{$v}%'";
			$i++;
		}
		echo $sql;
		$extend = $logic->extlists($sql,!empty($_GET['p'])?$_GET['p']:1,$row,'`cashcoupon` , `id` DESC',1,true);

		foreach ($extend as $v){

			$document = D('Document')->detail($v['id']);
			if($document){
				$list[] = array_merge($document,$v);
			}

		}

		//分页
		$this->assign('row',$row);
		$page = new \Common\Tool\Page($logic->listCountSearch($sql), $row);
		$page->setConfig('them','');
		$this->assign('_page',$page->show());
		$this->assign('list',$list);

		$this->display();
	}

	/* 文档模型列表页 */
	public function lists($p = 1){
		/* 分类信息 */
		$category = $this->category();

		/* 获取当前分类列表 */
		$Document = D('Document');
		$list = $Document->page($p, $category['list_row'])->lists($category['id']);
		if(false === $list){
			$this->error('获取列表数据失败！');
		}

		/* 模板赋值并渲染模板 */
		$this->assign('category', $category);
		$this->assign('list', $list);
		$this->display($category['template_lists']);
	}




	/* 发放优惠券 */
	public function give(){
		if(session("type") != 'Tmall'){
			$this->error('未登录或无权限，请重新确认登陆身份！',U('Home/User/login/type/'.session('type')));
		}
		$row = C('ROW');
		$this->assign('controller','Tmall');
		$this->assign('action','give');

		//审核操作
		//dump($_POST);
		if(isset($_POST['cashcoupon']) && $_POST['id']>0){
			switch ($_POST['cashcoupon']){
				case 0://未发放
					$data = array(
						'cashcoupon'	=>	0
					);
					break;
				case 1://已发放
					$data = array(
						'cashcoupon'	=>	1
					);
					//echo "开始审核";
					$_POST['givetime'] = time();
					break;
				case 2://已领取
					$_POST['receivetime'] = time();
					break;
			}
			if(D('Document')->logic(4)->update()){
				echo '修改成功';
			}
		}

		/* 获取扩展模型数据 */
		$logic  = D('Document')->logic(4);

		//dump($_GET);
		//是否搜索
		$sql = "(`cashcoupon`=0 OR `cashcoupon`=1) AND (`check`=1 OR `check`=2)";
		$i = 0;
		foreach ($_GET as $k=>$v){
			if($i>0 && 'p' != $k) $sql .= " AND ";
			if('p' != $k && ''==$sql) $sql .= "`{$k}` like '%{$v}%'";
			elseif('p' != $k) $sql .= " AND `{$k}` like '%{$v}%'";
			$i++;
		}


		//echo $sql;
		$extend = $logic->extlists($sql,!empty($_GET['p'])?$_GET['p']:1,$row,'cashcoupon,`id` DESC',1,true);

		foreach ($extend as $v){

			$document = D('Document')->detail($v['id']);
			if($document){
				$list[] = array_merge($document,$v);
			}

		}


		//分页
		$this->assign('row',$row);
		$page = new \Common\Tool\Page($logic->listCountSearch($sql), $row);
		$page->setConfig('them','');
		$this->assign('_page',$page->show());
		$this->assign('list',$list);


		$this->display();
	}


	/* 文档模型详情页 */
	public function detail($id = 0, $p = 1){
		if(session("type") != 'tmall'){
			exit();
		}
		/* 标识正确性检测 */
		if(!($id && is_numeric($id))){
			$this->error('文档ID错误！');
		}

		/* 页码检测 */
		$p = intval($p);
		$p = empty($p) ? 1 : $p;

		/* 获取详细信息 */
		$Document = D('Document');
		$info = $Document->detail($id);
		if(!$info){
			$this->error($Document->getError());
		}

		/* 分类信息 */
		$category = $this->category($info['category_id']);

		/* 获取模板 */
		if(!empty($info['template'])){//已定制模板
			$tmpl = $info['template'];
		} elseif (!empty($category['template_detail'])){ //分类已定制模板
			$tmpl = $category['template_detail'];
		} else { //使用默认模板
			$tmpl = 'Article/'. get_document_model($info['model_id'],'name') .'/detail';
		}

		/* 更新浏览数 */
		$map = array('id' => $id);
		$Document->where($map)->setInc('view');

		/* 模板赋值并渲染模板 */
		$this->assign('category', $category);
		$this->assign('info', $info);
		$this->assign('page', $p); //页码
		$this->display($tmpl);
	}

	/* 文档分类检测 */
	private function category($id = 0){
		/* 标识正确性检测 */
		$id = $id ? $id : I('get.category', 0);
		if(empty($id)){
			$this->error('没有指定文档分类！');
		}

		/* 获取分类信息 */
		$category = D('Category')->info($id);
		if($category && 1 == $category['status']){
			switch ($category['display']) {
				case 0:
					$this->error('该分类禁止显示！');
					break;
				//TODO: 更多分类显示状态判断
				default:
					return $category;
			}
		} else {
			$this->error('分类不存在或被禁用！');
		}
	}

}
